package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    //分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> p=setmealMapper.selectPage(setmealPageQueryDTO);

        return new PageResult(p.getTotal(),p.getResult());
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    //新增菜品
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        //添加套餐
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(setmealDTO.getCategoryId());
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insertSetmeal(setmeal);
        //获取到套餐id
        Long id = setmeal.getId();
        //添加套餐和菜品关系表
        //将套餐id添加到套餐和菜品种也就是SetmealDish中,将setmeal_dish中表数据补充完整
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        for (SetmealDish s : list) {
            s.setSetmealId(id);
        }
        //添加到套餐关系表中
        setmealMapper.insertSetmealDish(list);
    }


    //修改菜品状态
    @Override
    @Transactional
    public void setStatusById(Integer status, Long id) {
        /*//根据套餐id查找套餐,根据套餐里面的分类id找到对应的菜品(用一个集合返回)
        SetmealVO setmealById = setmealMapper.getSetmealById(id);
        Long categoryId = setmealById.getCategoryId();
        List<Dish> list=setmealMapper.getDishsById(categoryId);
        //遍历这个菜品集合,存在起售状态为 停售 的就抛异常
        for (Dish dish : list) {
            Integer status1 = dish.getStatus();
            if(status1==0)
            {
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        //否则就进行修改套餐的起售状态
        SetmealDTO setmealDTO=new SetmealDTO();
        setmealDTO.setId(id);
        setmealDTO.setStatus(status);*/

        //1.根据套餐id查找菜品
        Setmeal setmeal=new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        //2.修改套餐的起售状态
        setmealMapper.updateSetmeal(setmeal);

        //修改套餐的状态
        //setmealMapper.updateSetmeal();
    }

    //修改套餐
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //根据套餐id修改套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCategoryId(setmealDTO.getCategoryId());
        setmealDTO.getCategoryId();

        //修改套餐里面的菜品数据
        //1.清空原来的数据(根据套餐id清)
        setmealMapper.deleteSetmealDishes(setmealDTO.getId());
        //2.添加新的菜品数据
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        for (SetmealDish s : list) {//将套餐id添加到套餐菜品关系表中
            s.setSetmealId(setmealDTO.getId());
        }
        setmealMapper.insertSetmealDish(list);

        setmealMapper.updateSetmeal(setmeal);
    }

    //根据id查询套餐
    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO=setmealMapper.getSetmealById(id);
        //套餐和菜品的关联关系
        return setmealVO;
    }


    //删除套餐,根据集合
    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        //根据集合中的套餐id删除套餐
        for (Long id : ids) {
            //根据套餐id删除套餐
            setmealMapper.deleteSetmeal(id);
            //根据套餐Id 清空套餐和菜品关系数据
            setmealMapper.deleteSetmealDishes(id);
        }

    }


}
